package com.itboye.shop;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.itboye.shop.base.BaseFragment;
import com.itboye.shop.bean.ZiLeiMuBean;
import com.itboye.shop.category.adapter.MyListViewAdapter1;
import com.itboye.shop.category.adapter.MyListViewAdapter2;
import com.itboye.shop.category.store.GoodsListActivity;
import com.itboye.shop.net.ResultEntity;
import com.itboye.shop.presenter.UserPresenter;
import com.itboye.shop.util.loadutil.MAlert;

import java.util.List;
import java.util.Observable;
import java.util.Observer;


/**
 * 商城
 */
public class StoreFragment extends BaseFragment implements Observer {
    EditText editText;
    TextView tx_search;
    //    LoweLinearLayout store_chaiyoujiyou, store_luntai, store_famen, store_qipei, store_chengyongniaosu;
    LinearLayout layout;
    PopupWindow mPopWin;

    GridView goods_gridView;
    //    ShopMallSearchPresenter shopMallSearchPresenter;
//    List<ZiLeiMuBean> list;
//    StoreAdapter storeAdapter;
    LinearLayout kong_layoout;
    TextView kong_text;

    //    private List<SouSuoMall> listBean;
//    ShopMallSearchPresenter persenter;
//    SouSuoListAdapter adapter;
    LinearLayout shop;
    TextView store_line;
    private UserPresenter userPresenter;


    private int selectIndex = 0;

//    private static final String[] mMenus = {"常用分类", "服饰内衣", "鞋靴", "手机",
//            "家用电器", "数码", "电脑办公", "个护化妆", "图书", "二手手机", "数码", "电脑办公", "个护化妆", "图书", "二手手机"};
//    private String[] strs1 = {"常用分类1", "常用分类2", "常用分类3", "常用分类4", "常用分类5", "常用分类6", "常用分类7", "常用分类8", "常用分类9", "常用分类10"};
//    private String[] strs2 = {"服饰内衣1", "服饰内衣2", "服饰内衣3", "服饰内衣4", "服饰内衣5", "服饰内衣6", "服饰内衣7", "服饰内衣8", "服饰内衣9", "服饰内衣10", "服饰内衣11", "服饰内衣12", "服饰内衣13", "服饰内衣14", "服饰内衣15", "服饰内衣16"};
//    private String[] strs3 = {"鞋靴1", "鞋靴2", "鞋靴3", "鞋靴4", "鞋靴5", "鞋靴6"};
//    private String[] strs4 = {"手机1", "手机2", "手机3", "手机4"};
//    private String[] strs5 = {"家用电器1", "家用电器2", "家用电器3", "家用电器4", "家用电器5", "家用电器6", "家用电器7", "家用电器8"};
//    private String[][] allData = {strs1, strs2, strs3, strs4, strs5, strs1, strs2, strs3, strs4, strs5, strs1, strs2, strs3, strs4, strs5};
    private ListView list_item_1;
    GridView list_item_2;
    private MyListViewAdapter1 adapter1;
    private MyListViewAdapter2 adapter2;
    List<ZiLeiMuBean> list;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_store, null);
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        userPresenter = new UserPresenter(this);
        userPresenter.LeiMuQuery("1");
        editText.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {

                if ((event != null && event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN)) {
                    String searchText = editText.getText().toString();
                    if (searchText.equals("")) {
                        MAlert.alert("搜索内容为空");
                        return false;

                    }
//                    gotoGoodsList(searchText);
                    MAlert.alert("搜索中");
//                    persenter.getMallSouSuo(searchText);
                    return true;
                }
                return false;
            }
        });
        tx_search.setTextSize(15);
        initView();
    }

    private void initView() {

//        adapter1=new MyListViewAdapter1(mMenus,getActivity(),selectIndex);


        list_item_1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                selectIndex = position;
                //把下标传过去，然后刷新adapter
                adapter1.setIndex(position);
                adapter1.notifyDataSetChanged();
                //当点击某个item的时候让这个item自动滑动到listview的顶部(下面item够多，如果点击的是最后一个就不能到达顶部了)
                list_item_1.smoothScrollToPositionFromTop(position, 0);

                adapter2.setIndex(position);
                list_item_2.setAdapter(adapter2);
            }
        });

        list_item_2.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(getActivity(), GoodsListActivity.class);
                intent.putExtra("cate_id", list.get(selectIndex).getChildren().get(position).getId());
                startActivity(intent);
            }
        });
    }

    /**
     * 搜索出的内容
     */
    private void gouwuchePop() {
//        layout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(
//                R.layout.item_sousuo, null);
//        ListView sousuo_listview = (ListView) layout.findViewById(R.id.sousuo_listview);
//        LinearLayout kong_layoout = (LinearLayout) layout.findViewById(R.id.kong_layoout);
//        TextView kong_text = (TextView) layout.findViewById(R.id.kong_text);
//        if (listBean != null) {
//            if (listBean.size() > 0) {
//                adapter = new SouSuoListAdapter(getActivity(), listBean);
//                sousuo_listview.setAdapter(adapter);
//                //搜索出的商品列表点击
//                sousuo_listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                    @Override
//                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
////                        Intent intent = new Intent(getActivity(), GoodDetailActivity.class);
////                        intent.putExtra("goodsId", listBean.get(position).getId()+"");
////                        intent.putExtra("goodsName", listBean.get(position).getName());
////                        startActivity(intent);
//                    }
//                });
//
//            } else {
//                kong_layoout.setVisibility(View.VISIBLE);
//                sousuo_listview.setVisibility(View.GONE);
//                kong_text.setText("搜索内容为空");
//                return;
//            }
//        }
//
//        mPopWin = new PopupWindow(layout,
//                ViewGroup.LayoutParams.MATCH_PARENT,
//                ViewGroup.LayoutParams.WRAP_CONTENT, true);
//        mPopWin.setBackgroundDrawable(new BitmapDrawable());
//        if (!mPopWin.isShowing()) {
//            System.out.println("金磊磊额");
//            mPopWin.showAsDropDown(store_line, 5, 1);
//        }
    }

    void gotoGoodsList(String searchText) {
//        Intent intent = new Intent(getActivity(), GoodsListActivity.class);
//        intent.putExtra("searchText", searchText);
//        startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        String searchText = "";
        switch (v.getId()) {

            case R.id.tx_search:
                searchText = editText.getText().toString();
                if (searchText.equals("")) {
                    MAlert.alert("请输入搜索内容");
                    return;
                }
                MAlert.alert("搜索中");
//                persenter.getMallSouSuo(editText.getText().toString());

                break;

        }

    }

    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == UserPresenter.leimu_success) {
                //  ByAlert.alert(entity.getData());
                list = (List<ZiLeiMuBean>) entity.getData();
                if (list.size() > 0) {
                    adapter1 = new MyListViewAdapter1(list, getActivity(), selectIndex);
                    list_item_1.setAdapter(adapter1);
                    adapter2 = new MyListViewAdapter2(list, getActivity(), selectIndex);
                    list_item_2.setAdapter(adapter2);
//                    storeAdapter = new StoreAdapter(list, getActivity());
//                    goods_gridView.setAdapter(storeAdapter);
//                    goods_gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
//                        @Override
//                        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//                            Intent intent = new Intent(getActivity(), GoodsListActivity.class);
//                            intent.putExtra("cate_id", list.get(position).getId());
//                            startActivity(intent);
//                        }
//                    });
                } else {
                    goods_gridView.setVisibility(View.GONE);
                    kong_layoout.setVisibility(View.GONE);
                    kong_text.setText("物品为空");
                }

            } else if (entity.getEventType() == UserPresenter.leimu_fail) {
                MAlert.alert(entity.getData());
            }
//            else if (entity.getEventType() == ShopMallSearchPresenter.mall_sousuo_success) {
//                //ByAlert.alert(entity.getData());
//                listBean = (List<SouSuoMall>) entity.getData();
//                if (listBean == null) {
//                    ByAlert.alert("暂时没有搜索到相关商品");
//                } else if (listBean.size() <= 0) {
//                    ByAlert.alert("暂时没有搜索到相关商品");
//                } else {
//                    gouwuchePop();
//                }
//            } else if (entity.getEventType() == ShopMallSearchPresenter.mall_sousuo_fail) {
//                ByAlert.alert(entity.getData());
//            } else if (entity.getEventType() == UserPresenter.config_success) {
//                ConfigBean configbBean = (ConfigBean) entity.getData();
//                if (configbBean.getShopStatus().equals("0")) {
//                    shop.setVisibility(View.GONE);
//                    kong_layoout.setVisibility(View.VISIBLE);
//                } else {
//                    showProgressDialog("加载中,请稍后", true);
//                    shopMallSearchPresenter.LeiMuQuery("1");
//                    shop.setVisibility(View.VISIBLE);
//                    kong_layoout.setVisibility(View.GONE);
//                }
//            } else if (entity.getEventType() == UserPresenter.config_fail) {
//
//            }
        }
        try {
            closeProgressDialog();
        } catch (Exception e) {

        }
    }
}
